package com.pocketsweet.ui;

import java.math.BigDecimal;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.C;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLWithdraw;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_withdraw)
public class Withdraw extends BaseActivity {

	@ViewInject(R.id.etWithdraw)
	private EditText etWithdraw;

	@ViewInject(R.id.etAli)
	private EditText etAli;

	@ViewInject(R.id.tvWithdraw)
	private TextView tvWithdraw;

	@ViewInject(R.id.tvMyMoney)
	private TextView tvMyMoney;

	private HeaderLayout header;
	private double balance;
	private double peroid;
	private String money;
	private String alipay;
	public static LoadingDailog loading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		balance = getIntent().getDoubleExtra("balance", 0);
		initHead();
		peroid = balance * 10;
		if (peroid == 0) {
			tvMyMoney.setText("0时光");
		} else {
			tvMyMoney.setText((int)peroid + "时光");
//			tvMyMoney.setText(new BigDecimal(peroid).setScale(2,
//					BigDecimal.ROUND_HALF_UP) + "时光");
		}
		loading = ToolKits.createLoadingDialog(Withdraw.this, "提交中..");
		loading.setText("加载中");
	}

	@OnClick(R.id.tvWithdraw)
	public void onWithdrawBtnClick(View v) {
		money = etWithdraw.getText().toString();
//		money = String.valueOf((Integer.parseInt(money) / 10));
		alipay = etAli.getText().toString();
		if (TextUtils.isEmpty(money)) {
			ToolKits.toast(this, "您还没有填写提取的金额");
			return;
		}
		if (Integer.parseInt(money) > balance) {
			ToolKits.toast(this, "没有足够的金额");
			return;
		}
		if (Integer.parseInt(money) < 50) {
			ToolKits.toast(this, "提现金额不能少于50元");
			return;
		}
		if (TextUtils.isEmpty(alipay)) {
			ToolKits.toast(this, "您还没有填写您的支付宝账号");
			return;
		}
		loading.show();
		AVQuery<MLWithdraw> query = new AVQuery<MLWithdraw>("MLWithdraw");
		query.whereEqualTo("user", UserService.getCurrentUser());
		query.findInBackground(new FindCallback<MLWithdraw>() {

			@Override
			public void done(List<MLWithdraw> arg0, AVException e) {
				// TODO Auto-generated method stub
				if (e == null) {
					boolean isGetMoney = true;
					if (arg0.size() > 0) {
						MLWithdraw mlWithdraw;
						for (int i = 0; i < arg0.size(); i++) {
							mlWithdraw = arg0.get(i);
							if (mlWithdraw.getStatus() == C.WITHDRAW_STATUS_WAIT_PAY) {
								loading.dismiss();
								ToolKits.toast(Withdraw.this, "您已提交过申请，静静地等待吧");
								isGetMoney = false;
								break;
							}
							if (i == (arg0.size() - 1)) {
								isGetMoney = true;
							}
						}

					}
					if (isGetMoney) {
						// 提取成功,添加记录
						MLWithdraw withdraw = new MLWithdraw();
						withdraw.setSum(Integer.parseInt(money));
						withdraw.setAlipay(alipay);
						// withdraw.setName(name);
						withdraw.saveInBackground(new SaveCallback() {

							@Override
							public void done(AVException e) {
								if (e == null) {
									ToolKits.toast(Withdraw.this, "申请成功");
									Withdraw.this.finish();
									loading.dismiss();
									AVAnalytics.onEvent(Withdraw.this, "成功提现");
								} else {
									ToolKits.toast(Withdraw.this, "请重试");
									// ToolKits.toast(Withdraw.this,
									// e.getMessage());
									loading.dismiss();
								}
							}
						});
					} else {
						if (loading.isShowing()) {
							loading.dismiss();
						}
					}
				} else {
					ToolKits.toast(Withdraw.this, "请重试");
					loading.dismiss();
				}
			}
		});

		// if (TextUtils.isEmpty(name)) {
		// ToolKits.toast(this, "您还没有填写您的真实姓名");
		// return;
		// }

	}

	private void initHead() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("提现记录");
		header.setMiddleText("提现");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				Withdraw.this.finish();
			}
		});
		header.setRighttContainerClickListener(new onRightContainerListener() {

			@Override
			public void onClick() {
				// 跳到提现记录
				Intent intent = new Intent(Withdraw.this, WithdrawRecord.class);
				Withdraw.this.startActivity(intent);
			}
		});

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
